<?php
	class PostsController extends AppController{
		public $helper = array('Html' => 'Form' );
		public $componetns = array('Session');
		
		public function index(){
				$posts = $this->paginate();
				$this->set('posts', $posts);
		}		
		public function view($id = null){
			$this->layout = "empty";
			$this->set("title_for_layout","View user detail");
			$this->Post->id = $id;
			$this->set("post",$this->Post->read());
		}
		
		public function add(){
			if($this->request->is('post')){
				if($this->Post->save($this->request->data)){
					$this->Session->setFlash("This post was saved success");
					$this->redirect(array('action'=>'index'));
				}else{
					$this->Session->setFlash("Unable to save post");
				}
			}
		}
		
		public function edit($id = null ){
			$this->Post->id = $id;
			if($this->request->is('get')){
				$this->request->data = $this->Post->read();
			}else{
				if($this->Post->save($this->request->data)){
					$this->Session->setFlash("Your post have been updated.");
					$this->redirect(array('action' => 'index'));
				}else{
					$this->Session->setFlash("Unable to update your post");
				}
			}
		}
		public function delete($id){
			if($this->request->is('get')){
				throw new MethodNotAllowedException();
			}
			
			if($this->Post->delete($id)){
				$this->Session->setFlash("This post with id = " . $id . " have been deleted");
				$this->redirect(array('action' => 'index'));
			}
		}
		
		public function testRender(){
			$this->render("/Tests/test");
		}
		
		public function testRedirect(){
			$this->redirect(array("controller" => "thanks" , "action" => "success"));
		}
		
		public function testFlash(){
			$this->flash("This is test Flash message", array("controller" => "thanks", "action" => "success"), 1);
		}
		
		public function testRequestAction(){
			$this->autoRender = false;
			
			if(empty($this->request->params['requested'])){
				throw new ForbiddenException();
			}
			
			return  "This is just test";
		}
		
		public function  beforeFilter(){
			//$this->autoRender = false;
		}
		
		public function testDownload(){
			$this->viewClass = "Media";
			// Download app/outside_webroot_dir/example.zip
			$params = array(
					'id'        => 'example.zip',
					'name'      => 'example',
					'download'  => false,
					'extension' => 'zip',
					'path'      => APP . 'MediaFiles' . DS
			);
			$this->set($params);
		}
		
	}